//
//  ICRobotDetailViewController.m
//  ICome
//
//  Created by chenqs on 2017/7/21.
//  Copyright © 2017年 iCom. All rights reserved.
//

#import "ICRobotDetailViewController.h"
#import "ICRobotModel.h"
#import "ICContactsNetworkManager.h"
#import "ICHeaderFile.h"
static const CGFloat IconImageViewWH = 60.;
@interface ICRobotDetailViewController ()
@property (nonatomic, copy) NSString *botId;
@property (nonatomic, strong) NSTimer *chatTimer;
@end

@implementation ICRobotDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.model.botName;
    self.view.backgroundColor = BACKGROUNDCOLOR;
    
    if ([ICTools stringEmpty:(self.botId)] && [ICTools isObjEmpty:(self.model)]) {
        self.botId = self.parameterDictionary[@"eId"];
        [self sendData];
    }else
    {
        [self setupSubviews];
    }
}

- (void)sendData
{
    if ([ICTools stringEmpty:(self.botId)]) {
        return;
    }
    NSDictionary *dict = @{@"botId":self.botId};
    __weak typeof(self)weak_self = self;
    [MBProgressHUD showMessage:@"请稍候..." toView:self.view];
    [[ICContactsNetworkManager sharedInstance] getRobInfoWithParam:dict requestCache:^(id responseCache) {
        
        if (responseCache && [responseCache isKindOfClass:[ICRobotModel class]]) {
            
            weak_self.model = (ICRobotModel *)responseCache;
            weak_self.title = weak_self.model.botName;
            [weak_self setupSubviews];
        }
        [MBProgressHUD hideLoadingActivityView:weak_self.view];
    } success:^(id responseObject) {
        
        if (responseObject && [responseObject isKindOfClass:[ICRobotModel class]]) {
            
            weak_self.model = (ICRobotModel *)responseObject;
            weak_self.title = weak_self.model.botName;
            [weak_self setupSubviews];
        }
        [MBProgressHUD hideLoadingActivityView:weak_self.view];
    } failure:^(NSError *error) {
        
        [MBProgressHUD hideLoadingActivityView:weak_self.view];
        [MBProgressHUD showError:@"获取失败~"];
    }];
}

- (void)setupSubviews
{
    UIView *coverView = [[UIView alloc] initWithFrame:CGRectMake(10., 10., kScreenWidth-20., 0)];
    coverView.layer.cornerRadius = 8.;
    coverView.clipsToBounds = YES;
    coverView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:coverView];
    
    UIImageView *iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake((coverView.width-IconImageViewWH)/2, 20. , IconImageViewWH, IconImageViewWH)];
    iconImageView.image = [UIImage imageNamedFromiComeSDKBundle:@"calendar_meet"];
    iconImageView.layer.cornerRadius = 8.;
    iconImageView.clipsToBounds = YES;
    [coverView addSubview:iconImageView];
    
    //    UILabel *botIdLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, iconImageView.bottom +10, coverView.width, 13+2.)];
    //    botIdLabel.textAlignment = NSTextAlignmentCenter;
    //    botIdLabel.font = [UIFont systemFontOfSize:13];
    //    botIdLabel.textColor = [UIColor colorWithHexString:@"#999999"];
    //    [coverView addSubview:botIdLabel];
    
    
    UILabel *botNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, iconImageView.bottom +10+15., coverView.width, 24+2.)];
    botNameLabel.textAlignment = NSTextAlignmentCenter;
    botNameLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:24];;
    botNameLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    [coverView addSubview:botNameLabel];
    
    UILabel *botDescLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, botNameLabel.bottom +20, coverView.width-40, 0)];
    botDescLabel.textAlignment = NSTextAlignmentLeft;
    botDescLabel.numberOfLines = 0;
    botDescLabel.font = [UIFont systemFontOfSize:14];
    botDescLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    [coverView addSubview:botDescLabel];
    
    
    [iconImageView sd_setImageWithURL:[NSURL URLWithString:[ICTools getFileMaxImageURL:(self.model.photoId)]] placeholderImage:[UIImage imageNamedFromiComeSDKBundle:@"icon_ework_banner_default"] options:SDWebImageRetryFailed];
    //    botIdLabel.text = [NSString stringWithFormat:@"版本:%@",self.model.botId];
    botNameLabel.text = [NSString stringWithFormat:@"%@介绍",self.model.botName];
    botDescLabel.text = [NSString stringWithFormat:@"      %@",self.model.botDesc];
    
    CGSize botDescSize = [self sizeWithString:self.model.botDesc font:[UIFont systemFontOfSize:14] textWidth:coverView.width-40];
    botDescLabel.height = botDescSize.height;
    coverView.height = botDescLabel.bottom + 30.;
    
    [self setupChatBtn];
}

-(CGSize)sizeWithString:(NSString *)string font:(UIFont *)font textWidth:(CGFloat)width {
    
    NSDictionary *attrs1 = @{NSFontAttributeName:font};
    CGSize textMaxSize = CGSizeMake(width, MAXFLOAT);
    CGSize textSize = [string boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs1 context:nil].size;
    return textSize;
}

- (void)setupChatBtn
{
    UIView *toolView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight-59.-64., kScreenWidth, 59.)];
    toolView.backgroundColor = [UIColor colorWithHexString:@"#f7f7f8"];
    [self.view addSubview:toolView];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 0.5)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#acacac"];
    [toolView addSubview:lineView];
    
    UIButton *chatBtn = [[UIButton alloc] initWithFrame:CGRectMake((kScreenWidth-170.)/2, (toolView.height-44)/2., 170., 44.)];
    chatBtn.layer.borderWidth = 0.5;
    chatBtn.layer.borderColor = [UIColor colorWithHexString:@"#18cc90"].CGColor;
    chatBtn.layer.cornerRadius = 8.;
    chatBtn.clipsToBounds = YES;
    [chatBtn setTitle:@"和Ta对话" forState:UIControlStateNormal];
    chatBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [chatBtn setTitleColor:[UIColor colorWithHexString:@"#18cc90"] forState:UIControlStateNormal];
    [chatBtn setTitleColor:[UIColor colorWithHexString:@"#ffffff"] forState:UIControlStateHighlighted];
    [chatBtn setBackgroundImage:[UIImage imageWithColor:[UIColor whiteColor]] forState:UIControlStateNormal];
    [chatBtn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHexString:@"#18cc90"]] forState:UIControlStateHighlighted];
    [chatBtn addTarget:self action:@selector(chatBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [toolView addSubview:chatBtn];
}

- (void)chatBtnClick:(UIButton *)btn
{
    if (self.chatTimer) {
        [_chatTimer setFireDate:[NSDate distantFuture]];
    }
    
    _chatTimer = [NSTimer timerWithTimeInterval:0.25 target:self selector:@selector(chatBtnRequest:) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:_chatTimer forMode:NSRunLoopCommonModes];
}


- (void)chatBtnRequest:(NSTimer *)timer
{
    _chatTimer = nil;
    
    if ([ICTools isObjEmpty:(self.model)]) {
        [MBProgressHUD showError:@"目前无法为您服务~"];
        return;
    }
    NSDictionary *param = @{@"eId":[ICUser currentUser].eId,
                            @"eName":self.model.botName == nil?@"":self.model.botName,
                            @"otherId":self.model.botId};
    WEAKSELF
    [ICCreateGroupManager createTwoGroupWith:param Handle:^(ICGroup *group) {
        if (group) {
            ICChatViewController *vc = [[ICChatViewController alloc] init];
            vc.group = group;
//            [weakSelf pushMyViewController:vc animated:YES index:2];
//            [weakSelf pushToViewController:vc fromViewController:weakSelf animated:YES type:@""];
            [self.navigationController pushViewController:vc animated:YES];

        }
        
    }];
}


- (void)postNotification {
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationDidCreatedConversation object:nil];
}


- (void)dealloc
{
    [_chatTimer invalidate];
    _chatTimer = nil;
}
@end
